package com.gxa.jd.controller.back;

import com.gxa.exception.CustomerException;
import com.gxa.jd.pojo.Employee;
import com.gxa.jd.service.EmployeeService;
import com.gxa.entity.PageResult;
import com.gxa.entity.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@ResponseBody
@RequestMapping("/employee")
@Api(description = "后台登录接口文档")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ApiOperation(value = "登陆", notes = "登陆", httpMethod = "POST")
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功! data代表返回的数据"),
            @ApiResponse(code = 500, message = "后台逻辑错误!"),
            @ApiResponse(code = 404, message = "找不到页面!"),
            @ApiResponse(code = 405, message = "请求方式不正确，比如后台接收是post，但前端请求的是get!"),
            @ApiResponse(code = 406, message = "页面地址和接口名冲突!"),
            @ApiResponse(code = 415, message = "请求格式不正确，前端可能没有把请求头修改为json，也可能前端请求的json对象没有转换为字符串!")
    })
    public Result Login(@RequestParam String username,@RequestParam String password) throws CustomerException {
        Employee employee = employeeService.login(username, password);
        if (!password.equals(employee.getPassword())){
            return new Result(5003,"密码不正确");
        }
        System.out.println(employee.getPassword());
        System.out.println(password);
        return new Result(5008,"登录成功");

    }

    @GetMapping("/aaaaaaaa")
    public List<Employee> aaaaaa() {

        return employeeService.aaaaa();
    }


    @GetMapping("/findAll")
    public List<Employee> findAll() {
        return employeeService.findAll();
    }

    @GetMapping("/findPage")
    public PageResult<Employee> findPage(int page, int size) {
        return employeeService.findPage(page, size);
    }

    @PostMapping("/findList")
    public List<Employee> findList(@RequestBody Map<String, Object> searchMap) {
        return employeeService.findList(searchMap);
    }

    @PostMapping("/findPage")
    public PageResult<Employee> findPage(@RequestBody Map<String, Object> searchMap, int page, int size) {
        return employeeService.findPage(searchMap, page, size);
    }

    @GetMapping("/findById")
    public Employee findById(Long id) {
        return employeeService.findById(id);
    }


    @PostMapping("/add")
    public Result add(@RequestBody Employee employee) {
        employeeService.add(employee);
        return new Result();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Employee employee) {
        employeeService.update(employee);
        return new Result();
    }

    @GetMapping("/delete")
    public Result delete(Long id) {
        employeeService.delete(id);
        return new Result();
    }

}
